package com.example.xd.gamedemo.pingtu;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.xd.gamedemo.R;

import java.util.List;

/**
 * @auther XuDan on 2020/4/15.
 */
public class PingTuLayout extends FrameLayout implements View.OnClickListener, BaseQuickAdapter.OnItemClickListener {
    private int colum = 3;
    private TextView mTv_time;
    private RecyclerView mPingtu_rv;
    private Context mContext;
    private int mRv_height;
    private ImageInfo firstClick;
    private ImageInfo secondClick;

    public PingTuLayout(Context context) {
        this(context, null);
    }

    public PingTuLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PingTuLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        LayoutInflater.from(context).inflate(R.layout.pingtu_layout, this, true);
        findViewById(R.id.pingtu_btn_restart).setOnClickListener(this);
        findViewById(R.id.pingtu_btn_over).setOnClickListener(this);
        mTv_time = findViewById(R.id.pingtu_tv_time);
        mPingtu_rv = findViewById(R.id.pingtu_rv);

    }

    /**
     * 初始化图片
     *
     * @param pingtu_rv
     */
    private void initRecycle(RecyclerView pingtu_rv) {
        pingtu_rv.setLayoutManager(new GridLayoutManager(mContext, colum));
        Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.image10);
        List<ImageInfo> imgs = ClipBitmepUtil.getImgs(bitmap, colum);
        PingTuAdapter pingTuAdapter = new PingTuAdapter(mContext, R.layout.pingtu_layout_rv_item, imgs);
        pingtu_rv.setAdapter(pingTuAdapter);
        pingTuAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int measuredWidth = getMeasuredWidth();
        int measuredHeight = getMeasuredHeight();
        mRv_height = Math.min(measuredWidth, measuredHeight);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(mRv_height, mRv_height);
        mPingtu_rv.setLayoutParams(lp);
        initRecycle(mPingtu_rv);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.pingtu_btn_over:
                Toast.makeText(mContext, "结束", Toast.LENGTH_SHORT).show();
                break;
            case R.id.pingtu_btn_restart:
                Toast.makeText(mContext, "重新开始", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

    }
}
